/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.folder.stdPatterns.impl;

import net.yienyien.process.bpd.BPDComponent;
import net.yienyien.process.bpd.BPDObject;
import net.yienyien.process.bpd.BpdPackage;
import net.yienyien.process.bpd.ControlFlowRelation;
import net.yienyien.process.folder.stdPatterns.Repeat;
import net.yienyien.process.folder.stdPatterns.StdPatternsPackage;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Repeat</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class RepeatImpl extends StdBPDPatternImpl implements Repeat {
	
	public static final String NAME="Repeat";
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	protected RepeatImpl() {
		super(NAME, BpdPackage.eINSTANCE.getXORMerge(), BpdPackage.eINSTANCE.getDataBasedXOR(), BpdPackage.eINSTANCE.getTask(), BpdPackage.eINSTANCE.getIntermediateEvent());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return StdPatternsPackage.Literals.REPEAT;
	}

	@Override
	public EList<BPDObject> getContent(BPDObject source, BPDObject sink) {
		/*
		System.out.println("<Repeat>");
		System.out.println(source.getFrom());
		System.out.println(sink.getFrom());
		*/
		
		if(!isConnected(sink, source)) {
			return null;
		}
		
		//System.out.println("1.");
		
		if(source.getOutputs().size()!=1) {
			return null;
		}
		
		//System.out.println("2.");
		
		EList<BPDObject> set=new BasicEList<BPDObject>();
		set.add(source.getOutputs().iterator().next().getOutput());

		//System.out.println("3.");
		
		return set;
	}

	@Override
	public boolean verify(BPDComponent component) {
		if(component.getObjects().size()!=3 || component.getSource().getOutputs().size()!=1) {
			return false;
		}
		
		BPDObject x=null;
		for(BPDObject o: component.getObjects()) {
			if(!o.equals(component.getSource()) && !o.equals(component.getSink())) {
				x=o;
			}
		}
		
		if(x==null || x.getInputs().size()!=1 || x.getOutputs().size()!=1) {
			return false;
		}

		if(!x.equals(component.getSource().getOutputs().get(0).getOutput())) {
			return false;
		}
		if(!component.getSink().equals(x.getOutputs().get(0).getOutput())) {
			return false;
		}
		
		for(ControlFlowRelation r: component.getSink().getOutputs()) {
			if(r.getOutput().equals(component.getSource())) {
				return true;
			}
		}
		
		return false;
	}

} //RepeatImpl
